package com.tc.infinitechat.authenticationservice.exception;

import com.tc.infinitechat.authenticationservice.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName GlobalExceptionHandler
 * @Author 大马鲛鱼丸Gaga
 * @Description TODO
 * @Date 2025/10/8 15:26
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Result<?> handleBusinessException(BusinessException e) {
        log.warn("业务异常: {}", e.getMessage());
        return new Result<>()
                .setErrorCode(e.getCode())
                .setMessage(e.getMessage());
    }

    /**
     * 处理所有运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<?> handleRuntimeException(RuntimeException e) {
        log.error("运行时异常: ", e);
        return Result.fail("系统内部错误：" + e.getMessage());
    }

    /**
     * 兜底异常（防止漏网）
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        log.error("未知异常: ", e);
        return new Result<>()
                .setErrorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()))
                .setMessage("服务器异常，请联系管理员。");
    }
}
